package com.example.hewei.smarttransport.Fragment;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.hewei.smarttransport.R;
import com.example.hewei.smarttransport.Request.BaseRequest;
import com.example.hewei.smarttransport.Request.OriginalyRequest;
import com.example.hewei.smarttransport.Request.RequestThread;

/**
 * 创意入口
 */
public class OriginalyFragment extends BaseFragment {
    private final static String TAG = "OriginalyFragment";
    private RelativeLayout tempStoke;           //温度计的边框
    private TextView tempValueText;             //温度计显示的高度
    private TextView tempInfoText;              //温度计的摄氏度信息

    private TextView tempHIntText;              //根据温度提示用户搭乘哪种公交

    private RequestThread reqThread;            //发送请求的对象
    private OriginalyRequest originalyRequest;
    private ProgressDialog mDialog;             //进度框

    private int maxTemp = 42;                   //设置温度计最高温
    private int minTemp = -15;                  //设置温度计最低温


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_originaly, container, false);
    }

    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initViews();
        showDialog("正在获取温度信息");
        setRequest();
    }

    /**
     * 显示进度条
     */
    private void showDialog(String str) {
        if (mDialog == null) {
            mDialog = new ProgressDialog(getContext());
        }
        mDialog.setMessage(str);
        mDialog.show();
    }

    private void dismissDialog() {
        if (mDialog != null) {
            mDialog.dismiss();
            mDialog = null;
        }
    }

    //发送请求
    private void setRequest() {
        mApp.setServerIpStr("192.168.95.1");

        reqThread = new RequestThread(getContext(), mApp.getHandler());
        originalyRequest = new OriginalyRequest("");

        originalyRequest.setOnResponseEventListener(new BaseRequest.OnResponseEventListener() {
            @Override
            public void onResponse(BaseRequest request, BaseRequest.RequestResult result) {
                dismissDialog();
                if (originalyRequest.isSuccess())  //请求成功
                {
                    Log.d(TAG, "onResponse: " + originalyRequest.getTempValue());
                }
                setTempView(originalyRequest.getTempValue());
            }


        });

        reqThread.setRequest(originalyRequest);
        reqThread.start();
    }

    /**
     * 设置温度计的状态
     *
     * @param tempValue
     */
    private void setTempView(double tempValue) {
        //求出比例
        double scale = tempValue/(maxTemp - minTemp);
        if (tempValue > 28) {       //高温
            tempValueText.setBackgroundColor(getResources().getColor(R.color.app_red));
            tempStoke.setBackgroundResource(R.drawable.weather_bg_red);
            tempInfoText.setBackgroundResource(R.drawable.oral_red);
            tempHIntText.setText("天气炎热，建议搭乘空调巴士！");
        } else if (tempValue < 8)   //低温
        {
            tempValueText.setBackgroundColor(getResources().getColor(R.color.app_blue));
            tempStoke.setBackgroundResource(R.drawable.weather_bg_blue);
            tempInfoText.setBackgroundResource(R.drawable.oral_blue);
            tempHIntText.setText("天气寒冷，建议搭乘空调巴士！");
        }else{
            tempHIntText.setText("天气舒适，建议搭乘普通巴士！");
        }
        //设置温度计提示的高度
        tempValueText.setHeight((int) (tempStoke.getHeight() * scale));
        tempInfoText.setText(tempValue + "");
    }

    private void initViews() {
        tempStoke = (RelativeLayout) getView().findViewById(R.id.tempStoke);
        tempValueText = (TextView) getView().findViewById(R.id.tempValue);
        tempInfoText = (TextView) getView().findViewById(R.id.tempInfo);
        tempHIntText = (TextView) getView().findViewById(R.id.temp_hint_info);
    }

}
